package com.cryptoneedle.garden.service.dolphin;


import com.cryptoneedle.garden.dto.DolphinLocation;
import com.cryptoneedle.garden.dto.DolphinTaskDefinition;
import com.cryptoneedle.garden.dto.DolphinTaskRelation;
import com.cryptoneedle.garden.service.util.ConfigUtil;
import org.apache.dolphinscheduler.client.DolphinSchedulerClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>description:  </p>
 *
 * @author CryptoNeedle
 * @date 2025-02-28
 */
@Service
public class DolphinTaskDefinitionService {

    @Autowired
    private DolphinApiService dolphinApiService;

    @Autowired
    private DolphinSchedulerClient dolphinSchedulerClient;

    @Autowired
    private ConfigUtil configUtil;
    
    public DolphinTaskDefinition createTaskDefinition(Long projectCode, Long taskCode, String taskName, String script) {
        Date now = new Date();
        return new DolphinTaskDefinition()
                .setId(null)
                .setCode(taskCode)
                .setVersion(1)
                .setTaskType("SEATUNNEL")
                .setTaskExecuteType("BATCH")
                .setProjectCode(projectCode)
                .setProjectName(null)
                .setUserId(dolphinApiService.getUserCode())
                .setUserName(null)
                .setModifyBy(null)
                .setCreateTime(now)
                .setUpdateTime(now)
                .setOperateTime(now)
                .setOperator(4)
                .setTaskParamList(List.of())
                .setResourceIds(null)
                .setName(taskName)
                .setFlag("YES")
                .setIsCache("NO")
                .setDescription(null)
                .setTaskPriority("MEDIUM")
                .setWorkerGroup("default")
                .setEnvironmentCode(-1)
                .setTaskGroupId(0L)
                .setTaskGroupPriority(0)
                .setFailRetryTimes(0)
                .setFailRetryInterval(1)
                .setCpuQuota(-1)
                .setMemoryMax(-1)
                .setDelayTime(0)
                .setTimeoutFlag("CLOSE")
                .setTimeoutNotifyStrategy(null)
                .setTimeout(0)
                .setTaskParams(new DolphinTaskDefinition.TaskParam()
                                       .setStartupScript("seatunnel.sh")
                                       .setOthers(null)
                                       .setDeployMode("local")
                                       .setUseCustom(true)
                                       .setRawScript(script)
                                       .setLocalParams(List.of())
                                       .setResourceList(List.of()))
                .setTaskParamMap(null);
    }
    
    public DolphinTaskRelation createTaskRelation(Long definitionCode, DolphinTaskDefinition taskDefinition) {
        Date now = new Date();
        return new DolphinTaskRelation()
                .setId(null)
                .setName(null)
                .setProcessDefinitionVersion(taskDefinition.getVersion()) // todo 不确定是否要+1
                .setProjectCode(taskDefinition.getProjectCode())
                .setProcessDefinitionCode(definitionCode)
                .setPreTaskCode(0L)
                .setPreTaskVersion(0)
                .setPostTaskCode(taskDefinition.getCode())
                .setPostTaskVersion(taskDefinition.getVersion())
                .setConditionType("NONE")
                .setConditionParams(List.of())
                .setCreateTime(now)
                .setUpdateTime(now)
                .setOperateTime(now)
                .setOperator(4);
    }
    
    public DolphinLocation createLocation(DolphinTaskDefinition taskDefinition) {
        return new DolphinLocation()
                .setTaskCode(taskDefinition.getCode())
                .setX(300D)
                .setY(300D);
    }
}